package com.juyu.miliao.api.service;

import com.juyu.miliao.api.cache.GradeCache;
import com.juyu.miliao.api.dao.mysql.UserInfoDao;
import com.juyu.miliao.api.dao.redis.LineTimeRedisDao;
import com.juyu.miliao.api.dao.redis.UserRedisDao;
import com.juyu.miliao.api.domain.Grade;
import com.juyu.miliao.api.domain.User;
import com.juyu.miliao.api.domain.vo.WarnManUserVo;
import com.juyu.miliao.api.util.GradeUtil;
import com.juyu.miliao.api.util.RedisKeys;
import com.juyu.miliao.common.constant.EnumType;
import com.juyu.miliao.common.constant.OnlineUserRedis;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import com.juyu.miliao.common.util.ListGroupUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import static java.util.Comparator.comparing;
import static java.util.stream.Collectors.toList;

/**
 * @Auther: 卜加超
 * @Date: 2019/2/21 17:20
 * @Description:
 */
@Service
public class WarnManUserService {
    private static final Logger logger = LoggerFactory.getLogger(WarnManUserService.class);
    @Resource
    private UserInfoDao userInfoDao;
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    private LineTimeRedisDao lineTimeRedisDao;
    @Resource
    private UserRedisDao userRedisDao;

    /**
     * 查询打招呼非主播在线男用户方法
     * @param id 用户id
     * @return
     */
    public List<WarnManUserVo> getWarnManUser(long id) {
        User user = userInfoDao.getUserById(id);
        if (user.getIsHost() != 1)
            throw new ConsumeRuntimeException("用户不是主播");
        //获取在线用户前200个
        //TODO 写redisDao
        Set<String> range = userRedisDao.findOnLineUserBySex(EnumType.USER_SEX_MAN.state,0,200);
        if(range.size()<=0)
            return new ArrayList<>();
        List<Long> cids = range.stream().map(Long::parseLong).collect(toList());
        //随机取5条
         List<Long> randomList = ListGroupUtil.createRandomList(cids, 5);
//        for (long userId : randomList) {
//            User user1 = userInfoDao.getUserById(userId);
//            if (lineTimeRedisDao.hasKeyLine(user1.getUserId() + ""))
//                user1.setIsLine(1);
//            userList.add(user1);
//        }
        List<User> userList = userInfoDao.getQualityOnLineUserByUserIds(randomList);
        List<Grade> gradeList = GradeCache.INSTANCE.getGradeList(2);
        List<WarnManUserVo> wlist = new ArrayList<>();
        for (User user1 : userList) {
            if (lineTimeRedisDao.hasKeyLine(user1.getUserId() + ""))
                user1.setIsLine(1);
            WarnManUserVo warnManUserVo = new WarnManUserVo();
            warnManUserVo.setSex(user1.getSex());
            warnManUserVo.setUserId(user1.getUserId());
            warnManUserVo.setvGradeSum(user1.getvGradeSum());
            warnManUserVo.setvGrade(GradeUtil.getGradeCount(gradeList, user1.getvGradeSum()));
            warnManUserVo.setIsLine(user1.getIsLine());
            warnManUserVo.setAge(user1.getAge());
            warnManUserVo.setNickName(user1.getNickName());
            warnManUserVo.setIcon(user1.getIcon());
            wlist.add(warnManUserVo);
        }
        List<WarnManUserVo> lineUserList = wlist.stream()
                .filter(i -> i.getIsLine() == 1)
                .sorted(comparing(WarnManUserVo::getvGradeSum).reversed())
                .collect(toList());
        List<WarnManUserVo> collect = wlist.stream()
                .filter(i -> i.getIsLine() != 1)
                .sorted(comparing(WarnManUserVo::getvGradeSum).reversed()).collect(toList());
        lineUserList.addAll(collect);
        return lineUserList;
    }

}
